import time
import allure
import pytest
import requests
import os
from .data import load_addPatients,baseurl
basedir=os.path.dirname(__file__)
filepath=os.path.join(basedir,"testCase/addPatients.json")
@allure.epic("添加患者接口")
class TestaddPatients:
    def setup_class(self):
        self.session=requests.session()
        self.url = f"{baseurl}/patients/add_by_phone"
        print("添加患者接口测试开始：", time.strftime("%Y-%m-%d-%H:%M:%S"))
    def teardown_class(self):
        self.session.close()
        print("添加患者接口测试结束：", time.strftime("%Y-%m-%d-%H:%M:%S"))
    @allure.story("医生访问该接口")
    @pytest.mark.parametrize("testCase,phone,msg,status_code",load_addPatients(filepath))
    @allure.title("{testCase}")
    def test_addPatients(self,testCase,phone,msg,status_code):
        self.session.post(f"{baseurl}/login", data={"username": "doctor", "password": "111111"})
        data={
            "phone":phone
        }
        responses=self.session.post(self.url,data=data)
        assert msg in responses.text
        assert status_code==responses.status_code
        self.session.cookies.clear()

    @allure.story("其他用户访问该接口")
    @allure.title("普通用户添加患者")
    def test_AtherroleVisit1(self):
        self.session.post(f"{baseurl}/login", data={"username": "test_addPatient1", "password": "111111"})
        responses=self.session.post(self.url,data={"phone":"17322975495"})
        print(responses.text)
        assert "无权访问此功能" in responses.text
        assert responses.status_code==200
        self.session.cookies.clear()


    @allure.title("管理员添加患者")
    def test_AtherroleVisit2(self):
        self.session.post(f"{baseurl}/login", data={"username": "manage", "password": "111111"})
        responses = self.session.post(self.url, data={"phone": "17322975495"})
        assert "无权访问此功能" in responses.text
        assert responses.status_code == 200
        self.session.cookies.clear()


    @allure.title("超级管理员添加患者")
    def test_AtherroleVisit3(self):
        self.session.post(f"{baseurl}/login", data={"username": "root", "password": "111111"})
        responses = self.session.post(self.url, data={"phone": "17322975495"})
        assert "无权访问此功能" in responses.text
        assert responses.status_code == 200
        self.session.cookies.clear()



